package com.temp.controller.admin;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.temp.dto.RoleDTO;
import com.temp.dto.RolePermDTO;
import com.temp.dto.RouteRoleDTO;
import com.temp.dto.SearchRoleDTO;
import com.temp.entity.Role;
import com.temp.entity.RolePermission;
import com.temp.entity.RouteRole;
import com.temp.result.Result;
import com.temp.service.RolePermissionService;
import com.temp.service.RoleService;
import com.temp.service.RouteRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/api/admin/role")
@Api(tags = "管理角色相关接口")
@Slf4j
@Validated
public class AdminRoleController {

    @Autowired
    private RoleService roleService;

    @Autowired
    private RolePermissionService rolePermissionService;

    @Autowired
    private RouteRoleService routeRoleService;

    @PostMapping("/query")
    @ApiOperation("管理员获取角色信息")
    @RequiresPermissions("admin:role:query")
    public Result getAllRole(@RequestBody SearchRoleDTO roleDTO) {
        roleDTO.formatParam();
        return Result.success(roleService.findAllRoles(roleDTO),"apiMsg.querySuccess");
    }

    @PostMapping("/updateOrSave")
    @ApiOperation("保存或者更新角色")
    @RequiresPermissions("admin:role:updateOrSave")
    public Result updateOrSave(@Valid @RequestBody RoleDTO roleDTO) {
        Role role = new Role();
        BeanUtils.copyProperties(roleDTO, role);
        boolean isSuccess = roleService.saveOrUpdate(role);

        return isSuccess ? Result.success("apiMsg.optionSuccess") : Result.error("apiMsg.optionError");
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除角色")
    @RequiresPermissions("admin:role:delete")
    public Result delCategory(@RequestParam Long id) {
        rolePermissionService.remove(Wrappers.<RolePermission>lambdaQuery().eq(RolePermission::getRoleId, id));
        routeRoleService.remove(Wrappers.<RouteRole>lambdaQuery().eq(RouteRole::getRoleId, id));
        boolean isSuccess = roleService.removeById(id);

        return isSuccess ? Result.success("apiMsg.optionSuccess") : Result.error("apiMsg.optionError");
    }

    @GetMapping("/queryPerm")
    @ApiOperation("查询角色具有的权限编号")
    @RequiresPermissions("admin:role:queryPerm")
    public Result getRolePerm(@RequestParam Long roleId) {
        List<RolePermission> list = rolePermissionService.list(Wrappers.<RolePermission>lambdaQuery().eq(RolePermission::getRoleId, roleId));
        List<Long> collect = list.stream().map(RolePermission::getPermissionId).collect(Collectors.toList());
        return Result.success(collect,"apiMsg.querySuccess");
    }

    @GetMapping("/queryRoute")
    @ApiOperation("查询角色具有的路由编号")
    @RequiresPermissions("admin:role:queryRoute")
    public Result getRoleRoute(@RequestParam Long roleId) {
        List<RouteRole> list = routeRoleService.list(Wrappers.<RouteRole>lambdaQuery().eq(RouteRole::getRoleId, roleId));
        List<Long> collect = list.stream().map(RouteRole::getRouteId).collect(Collectors.toList());
        return Result.success(collect,"apiMsg.querySuccess");
    }

    @PostMapping("/savePerm")
    @ApiOperation("保存角色和权限的关系")
    @RequiresPermissions("admin:role:savePerm")
    public Result saveRolePerm(@RequestBody RolePermDTO rolePermDTO) {
        rolePermissionService.remove(Wrappers.<RolePermission>lambdaQuery().eq(RolePermission::getRoleId, rolePermDTO.getRoleId()));
        if(rolePermDTO.getPermIds().size() == 0) {
            return Result.success("apiMsg.optionSuccess");
        } else {
            List<RolePermission> collect = rolePermDTO.getPermIds().stream().map(p -> RolePermission.builder().roleId(rolePermDTO.getRoleId()).permissionId(p).build()).collect(Collectors.toList());
            boolean isSuccess = rolePermissionService.saveBatch(collect);
            return isSuccess ? Result.success("apiMsg.optionSuccess") : Result.error("apiMsg.optionError");
        }
    }

    @PostMapping("/saveRoute")
    @ApiOperation("保存角色和路由的关系")
    @RequiresPermissions("admin:role:saveRoute")
    public Result saveRoleRoute(@RequestBody RouteRoleDTO routeRoleDTO) {
        routeRoleService.remove(Wrappers.<RouteRole>lambdaQuery().eq(RouteRole::getRoleId, routeRoleDTO.getRoleId()));
        if(routeRoleDTO.getRouteIds().size() == 0) {
            return Result.success("apiMsg.optionSuccess");
        } else {
            List<RouteRole> collect = routeRoleDTO.getRouteIds().stream().map(p -> RouteRole.builder().roleId(routeRoleDTO.getRoleId()).routeId(p).build()).collect(Collectors.toList());
            boolean isSuccess = routeRoleService.saveBatch(collect);
            return isSuccess ? Result.success("apiMsg.optionSuccess") : Result.error("apiMsg.optionError");
        }
    }
}
